package com.keldian.zhbj74.base.impl;

import java.util.ArrayList;

import com.google.gson.Gson;
import com.keldian.zhbj74.activity.MainActivity;
import com.keldian.zhbj74.base.BaseMenuDetailPager;
import com.keldian.zhbj74.base.BasePager;
import com.keldian.zhbj74.base.impl.menu.InteractMenuDetailPager;
import com.keldian.zhbj74.base.impl.menu.NewsMenuDetailPager;
import com.keldian.zhbj74.base.impl.menu.PhotosMenuDetailPager;
import com.keldian.zhbj74.base.impl.menu.TopicMenuDetailPager;
import com.keldian.zhbj74.domain.NewsMenu;
import com.keldian.zhbj74.fragment.LeftMenuFragment;
import com.keldian.zhbj74.global.GlobalConstants;
import com.keldian.zhbj74.utils.CacheUtils;
import com.keldian.zhbj74.utils.SharePrefencesUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 新闻中心
 * 
 * @author Kevin
 * @date 2015-10-18
 */
public class NewsCenterPager extends BasePager {
	
	private ArrayList<BaseMenuDetailPager> mMenuDetailPagers;
	private NewsMenu mNewsMenu;
	private int mCurrentPosition;
	
	

	public NewsCenterPager(Activity activity) {
		super(activity);
		
	}

	@Override
	public void initData() {
		System.out.println("新闻中心初始化啦...");

		// // 要给帧布局填充布局对象
		// TextView view = new TextView(mActivity);
		// view.setText("新闻中心");
		// view.setTextColor(Color.RED);
		// view.setTextSize(22);
		// view.setGravity(Gravity.CENTER);
		//
		// flContent.addView(view);

		// 修改页面标题
		tvTitle.setText("新闻");
		btnMenu.setVisibility(View.VISIBLE);

		String cacheData = CacheUtils.getCache(GlobalConstants.CATEGORY_URL,
				mActivity);
		if (TextUtils.isEmpty(cacheData)) {
			System.out.println(cacheData+"-------------");
			getDataFromServer();
		}else{
			System.out.println("发现缓存~");
			System.out.println(cacheData+"-------------");
			processData(cacheData);
		}
		
	}

	private void getDataFromServer() {
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.GET, GlobalConstants.CATEGORY_URL,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						// 请求成功
						String result = responseInfo.result;// 获取服务器返回结果
						System.out.println("服务器返回结果:" + result);
						CacheUtils.setCache(GlobalConstants.CATEGORY_URL, result, mActivity);
						processData(result);
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// 请求失败
						error.printStackTrace();
						Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT)
								.show();
					}
				});
	}

	private void processData(String result) {
		Gson gson = new Gson();
		mNewsMenu = gson.fromJson(result, NewsMenu.class);
		MainActivity mainUI = (MainActivity) mActivity;
		LeftMenuFragment leftMenuFragment = mainUI.getLeftMenuFragment("TAG_LEFT_MENU");
		
		leftMenuFragment.setMenuData(mNewsMenu.data);
		mMenuDetailPagers = new ArrayList<BaseMenuDetailPager>();
		mMenuDetailPagers.add(new NewsMenuDetailPager(mActivity, mNewsMenu.data.get(0).children));
		mMenuDetailPagers.add(new TopicMenuDetailPager(mActivity));
		mMenuDetailPagers.add(new PhotosMenuDetailPager(mActivity, btnPhoto));
		mMenuDetailPagers.add(new InteractMenuDetailPager(mActivity));
		
		mCurrentPosition = SharePrefencesUtils.getInt(mActivity, "MENU_POSITION", 0);
		System.out.println(mCurrentPosition+"+++");
		setCurrentDetailPager(mCurrentPosition);
	}
	
	public void setCurrentDetailPager(int position){
		SharePrefencesUtils.setInt(mActivity, "MENU_POSITION", position);
		mCurrentPosition = SharePrefencesUtils.getInt(mActivity, "MENU_POSITION", 0);
		System.out.println(mCurrentPosition+"-----");
		
		BaseMenuDetailPager pager = mMenuDetailPagers.get(position);
		View view = pager.mRootView;
		flContent.removeAllViews();
		flContent.addView(view);
		pager.initData();
		
		tvTitle.setText(mNewsMenu.data.get(position).title);
		 
		if (pager instanceof PhotosMenuDetailPager) {
			btnPhoto.setVisibility(View.VISIBLE);
		} else {
			// 隐藏切换按钮
			btnPhoto.setVisibility(View.GONE);
		}
	}
	
}
